var utils = {};

utils.empty = function(obj) {
	var is = (typeof obj == "undefined") || obj === "" || obj === false || obj === null;
	return is;
};

utils.getFieldsData = function(klass) {
	
	var fields = $("." + klass);
	
	var data = {};
	
	$.each(fields, function (i, field) {
		data[field.name] = field.value;
	});

	return data;
};

utils.truncate = function(str, length) {
	return str.length > length ? str.substring(0, length) + "..." : str;
};

utils.render = function(template, params) {
	params.cdn_url = config.cache_path;
	return Mustache.to_html(template, params);
};

utils.getText = function(key, params) {
    var template = lang.text[key];
    
    if (typeof template == String(undefined)) {
        return key;
    }
    
    if (utils.empty(params)) return template;
    
    $.each(params, function(n, param) {
    	template = template.replace("%" + n + "%", param);
    });
    
    return template;
};

// shortcut
var gt = utils.getText;

utils.flatten = function(list) {
	
	var flatten = new Array();
	
	$.each(list, function(i, item) {
		flatten.push(item);
	});
	
	return flatten;
};

utils.toList = function(items) {
	
	if (!$.isArray(items)) {
		var tmp = new Array();
		tmp[0] = items;
		return tmp;
	}
	
	return items;
}

function loadExternalScript(url) {
	  var script = document.createElement("script");
	  script.src = url;
	  script.type = "text/javascript";
	  document.getElementsByTagName("head")[0].appendChild(script);
}
